package com.microframework.boot.visual.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import com.microframework.boot.visual.model.domain.MaterialImage;
import com.microframework.boot.visual.service.IMaterialImageService;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MaterialImageServiceImpl implements IMaterialImageService{

	@Override
	public Page<MaterialImage> pages(String categoryId,String keywords, int page, int pageSize) {
		
		String select="select * ";
		StringBuilder sqlBuilder = new StringBuilder("FROM " + MaterialImage.TABLE_NAME + " WHERE 1=1");
		List<Object> params = new ArrayList<>();
		if (StringUtils.hasText(categoryId)) {
			sqlBuilder.append(" AND category_id = ?");
			params.add(categoryId);
		}
		if (StringUtils.hasText(keywords)) {
			sqlBuilder.append(" AND name LIKE ?");
			params.add("%" +keywords + "%");
		}
		sqlBuilder.append(" AND is_del = ?");
		params.add("0");

		sqlBuilder.append(" ORDER BY modify_time desc ");
		return MaterialImage.dao.paginate(page, pageSize, select,sqlBuilder.toString(), params.toArray());
	}
}
